//
//  FileHelper.m
//  Etion
//
//  Created by  user on 11-2-8.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "XMSFileHelper.h"

//#import "ErrorCode.h"

static NSString *s_szDocument;

//static NSString* s_szTemp;

@implementation XMSFileHelper

+ (void)initPath
{
    if (nil == s_szDocument)
        s_szDocument = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] retain_mm];
//	s_szTemp=[[NSHomeDirectory() stringByAppendingPathComponent:@"Temp"]retain];
}

+ (NSString *)documentPath
{
    return s_szDocument;
}

+ (NSString *)tempPath
{
//	return NSTemporaryDirectory();

#if !(TARGET_IPHONE_SIMULATOR)
    return NSTemporaryDirectory();
#else
    return [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
#endif
}

+ (void)createDir:(NSString *)szPath
{
    [[NSFileManager defaultManager] createDirectoryAtPath:szPath withIntermediateDirectories:NO attributes:nil error:nil];
}

//+ (NSInteger)isDirEmpty:(NSString *)szPath files:(NSArray **)arFile
//{
//    NSInteger nCount = 0;
//    NSArray *arList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:szPath error:nil];
//    if (arFile != nil)
//        (*arFile) = [arList retain_mm];
//    nCount = arList.count;
//    if ([arList containsObject:@".DS_Store"])
//    {
//        nCount--;
//    }
//    return nCount;
//}

+ (NSInteger)isDirEmpty:(NSString *)szPath containFiles:(NSArray **)arFile
{
    NSInteger nCount = 0;
    NSArray *arList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:szPath error:nil];
    
    if ([arList containsObject:@".DS_Store"])
    {
        NSMutableArray *arT = [[NSMutableArray new]autorelease_mm];
        [arT addObjectsFromArray:arList];
        [arT removeObject:@".DS_Store"];
        arList = arT;
    }
    
    if (arFile != nil)
        (*arFile) = arList;
    nCount = arList.count;
    
    return nCount;
}

+ (NSInteger)isFileDirExist:(NSString *)szPath
{
    NSInteger nResult = NO;
    nResult = (YES== [[NSFileManager defaultManager] fileExistsAtPath:szPath] ? TRUE : nResult);
    return nResult;
}

+ (NSInteger)isFileExist:(NSString *)szPath
{
    NSInteger nResult = NO;
    BOOL bIsdir = NO;
    nResult = (YES== [[NSFileManager defaultManager] fileExistsAtPath:szPath isDirectory:&bIsdir] ? TRUE : nResult);
    return (nResult == YES&& bIsdir == NO) ? YES : NO;
}

+ (NSInteger)isDirExist:(NSString *)szPath
{
    NSInteger nResult = NO;
    BOOL bIsdir = NO;
    nResult = (YES== [[NSFileManager defaultManager] fileExistsAtPath:szPath isDirectory:&bIsdir] ? TRUE : nResult);
    return (nResult == YES&& bIsdir == YES) ? YES : NO;
}

+ (BOOL)deleteFileDir:(NSString *)szPath
{
    return [[NSFileManager defaultManager] removeItemAtPath:szPath error:nil];
}

+ (long long)getFileSize:(NSString *)szPath
{
    long long nFilesize = 0;
    NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:szPath];
    nFilesize = [handle seekToEndOfFile];
    [handle closeFile];
    return nFilesize;
}

+ (void)copySource:(NSString *)szSrcpath toTarget:(NSString *)szTarpath
{
    [[NSFileManager defaultManager] copyItemAtPath:szSrcpath toPath:szTarpath error:nil];
}

+ (void)moveSource:(NSString *)szSrcpath toTarget:(NSString *)szTarpath
{
    [[NSFileManager defaultManager] moveItemAtPath:szSrcpath toPath:szTarpath error:nil];
}

+ (NSInteger)enumArchiveFileToObjs:(NSString *)szPath array:(NSMutableArray **)objs
{
    NSInteger nResult = FALSE;

//	NSArray* arList=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:szPath error:nil];

    NSArray *arList = nil;

    [self isDirEmpty:szPath containFiles:&arList];

    if (arList != nil&& arList.count > 0)
    {
//#if TARGET_OS_IPHONE
//		nResult=TRUE;
//		(*objs)=[[NSMutableArray alloc]initWithCapacity:arList.count];
//		for(NSInteger i=0;i<arList.count;i++)
//		{
//			NSString* p=[arList objectAtIndex:i];
//			NSString* pp=[szPath stringByAppendingPathComponent:p];
//			[(*objs) addObject:[NSKeyedUnarchiver unarchiveObjectWithFile:pp]];
//		}
//#else if TARGET_IPHONE_SIMULATOR
        nResult = TRUE;
        (*objs) = [NSMutableArray new];
        for (NSInteger i = 0; i < arList.count; i++)
        {
            NSString *p = [arList objectAtIndex:i];
            //模拟器上会出现
            if (0 != [p compare:@".DS_Store"])
            {
                NSString *pp = [szPath stringByAppendingPathComponent:p];
                [(*objs) addObject:[NSKeyedUnarchiver unarchiveObjectWithFile:pp]];
            }
        }
//#endif
    }
    return nResult;
}

+ (NSDictionary *)getFileAttributes:(NSString *)szPath
{
    return [[NSFileManager defaultManager] attributesOfItemAtPath:szPath error:nil];
}

+ (long long)getSystemFreeSize
{
    NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    NSNumber *freesize = [dic objectForKey:NSFileSystemFreeSize];
    return [freesize longLongValue];
}

+ (long long)getSystemTotalSize
{
    NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    NSNumber *size = [dic objectForKey:NSFileSystemSize];
    return [size longLongValue];
}

+ (long long)getSystemUsedSize
{
    NSDictionary *dic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    NSNumber *freesize = [dic objectForKey:NSFileSystemFreeSize];
    NSNumber *size = [dic objectForKey:NSFileSystemSize];
    return [size longLongValue] - [freesize longLongValue];
}

@end
